﻿import { Component, EventEmitter, Injector, Output, ViewChild } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { RoleUserServiceProxy,RoleUserDto,OrganizationUnitDto, PasswordComplexitySetting, ProfileServiceProxy, CheckerRoleEnum, UserRoleDto, UserServiceProxy,DepartmentServiceProxy } from '@shared/service-proxies/service-proxies';
import { ModalDirective } from 'ngx-bootstrap';
import { finalize } from 'rxjs/operators';
import { RequirementPackageServiceProxy} from '@shared/service-proxies/service-proxies';
import { AbpSessionService } from '@abp/session/abp-session.service';
@Component({
    selector: 'auditAuthorityEditComponent',
    templateUrl: './audit-authority_edit.component.html'
})
export class AuditAuthorityEditComponent extends AppComponentBase {

    @ViewChild('createOrEditModal') modal: ModalDirective;

    @Output() modalSave: EventEmitter<any> = new EventEmitter<any>();

    active = false;
    saving = false;
    canChangeUserName = true;
    isTwoFactorEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.TwoFactorLogin.IsEnabled');
    isLockoutEnabled: boolean = this.setting.getBoolean('Abp.Zero.UserManagement.UserLockOut.IsEnabled');
    passwordComplexitySetting: PasswordComplexitySetting = new PasswordComplexitySetting();

    //user: UserEditDto = new UserEditDto();
    record: RoleUserDto = new RoleUserDto();
    roles: UserRoleDto[];
    sendActivationEmail = true;
    setRandomPassword = true;
    passwordComplexityInfo = '';
    profilePicture: string;

    allOrganizationUnits: OrganizationUnitDto[];
    memberedOrganizationUnits: string[];
    editFlag = true;
    roleOption:object[];
    unitOfUsings: Array<object>;
    allUserOption:Array<object>;
    id: number;

    constructor(
        injector: Injector,
        private _userService: UserServiceProxy,
        private _requirementPackageService: RequirementPackageServiceProxy,
        private _departmentServiceProxy:DepartmentServiceProxy,
        private _roleUserServiceProxy:RoleUserServiceProxy,
        private _sessionServiceProxy: AbpSessionService,
        private _profileService: ProfileServiceProxy
    ) {
        super(injector);
        this.roleOption = new Array<object>()
        this.roleOption.push({value:0,text:CheckerRoleEnum[0]});
        this.roleOption.push({value:1,text:CheckerRoleEnum[1]});
        this.roleOption.push({value:2,text:CheckerRoleEnum[2]});
        this.roleOption.push({value:3,text:CheckerRoleEnum[3]});
        this.getunitOfUsings();
        this.getAllUserOption();
    }
    getunitOfUsings() {
        this.unitOfUsings = new Array<object>()
        this._departmentServiceProxy.getAllDpts()
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.unitOfUsings = result;
            });
    }
    getAllUserOption() {
        this.allUserOption = new Array<object>()
        this._roleUserServiceProxy.getAllUsers()
            .pipe(finalize(() => this.primengTableHelper.hideLoadingIndicator())).subscribe(result => {
                this.allUserOption = result;
            });
    }
    uuid() {
        var uuidValue = "", k, randomValue;
        for (k = 0; k < 32;k++) {
          randomValue = Math.random() * 16 | 0;
      
          if (k == 8 || k == 12 || k == 16 || k == 20) {
            uuidValue += "-"
          }
          uuidValue += (k == 12 ? 4 : (k == 16 ? (randomValue & 3 | 8) : randomValue)).toString(16);
        }
        return uuidValue;
      }
    show(_pRoleUserDto?: RoleUserDto): void {
        if (_pRoleUserDto) {
            this.record = _pRoleUserDto;
            this.id = _pRoleUserDto.id;
        } else {
            this.record = new RoleUserDto();
            this.record.role = 0;
        }
        this.active = true;
        this.modal.show();
    }

    onShown(): void {
        
    }

    save(): void {
        this._roleUserServiceProxy.createUserOfRole(this.record)
            .pipe(finalize(() => { this.saving = false; }))
            .subscribe(() => {
                this.notify.info(this.l('SavedSuccessfully'));
                this.close();
                this.modalSave.emit(null);
            });
    }

    close(): void {
        this.active = false;
        this.modal.hide();
    }

}
